package com.groupbuying.settleproducer.service.impl;
import com.groupbuying.settleproducer.connector.SysconfigFeign;
import com.groupbuying.settleproducer.constants.SettleWithdrawalEnumConstants;
import com.groupbuying.settleproducer.dao.SettleUshopWithdrawalDao;
import com.groupbuying.settleproducer.domain.SettleUshopWithdrawalDO;
import com.groupbuying.settleproducer.service.*;
import com.groupbuying.settleproducer.utils.apiresult.ApiResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
/**
 * @Author: WANGNING
 * @Email wyn20140226@aliyun.com
 * @Date: 2019/6/3 16:05
 * @Des 提现总接口
 */

@Service
public class SettleUshopWithdrawalServiceImpl implements SettleUshopWithdrawalService {
    public static final Logger logger = LoggerFactory.getLogger(SettleUshopWithdrawalServiceImpl.class);
    @Autowired
    private SettleUshopWithdrawalDao settleUshopWithdrawalDao;
    @Autowired
    private SysconfigFeign sysconfigFeign;
    @Autowired
    private SettleUshopYSWithdrawalService ysWithdrawalService;
    @Autowired
    private SettleUshopPaymentService ysPaymentService;
    @Autowired
    private SettleUshopNativePaymentService nativePaymentService;
    @Autowired
    private SettleUshopFirstClearAndBalanceService settleUshopFirstClearAndBalanceService;
    @Autowired
    private SettleUshopBalanceAndIntegralService settleUshopBalanceAndIntegralService;
    @Override
    public SettleUshopWithdrawalDO get(String withdrawalId) {
        return settleUshopWithdrawalDao.get(withdrawalId);
    }

    @Override
    public List<SettleUshopWithdrawalDO> list(Map<String, Object> map) {
        return settleUshopWithdrawalDao.list(map);
    }

    @Override
    public int count(Map<String, Object> map) {
        return settleUshopWithdrawalDao.count(map);
    }

    @Override
    public int save(SettleUshopWithdrawalDO settleWithdrawal) {
        return settleUshopWithdrawalDao.save(settleWithdrawal);
    }

    @Override
    public int update(SettleUshopWithdrawalDO settleWithdrawal) {
        return settleUshopWithdrawalDao.update(settleWithdrawal);
    }

    @Override
    public int remove(String withdrawalId) {
        return settleUshopWithdrawalDao.remove(withdrawalId);
    }

    @Override
    public int batchRemove(String[] withdrawalIds) {
        return settleUshopWithdrawalDao.batchRemove(withdrawalIds);
    }

    /**
     * @param params
     * @return com.groupbuying.settleproducer.utils.apiresult.ApiResult
     * @des 提现业务总接口
     * @date 2019/6/3
     */
    @Override
    public ApiResult createWithdrawal(Map<String, Object> params) {
       /* //查询商户中心获取门店ID和ruleId
        String counterType = (String) params.get("ysUserStatus");
        logger.info("SettleUshopWithdrawalServiceImpl.createWithdrawal()", "该商户类型为（0是非一清代付;2是银盛一清;1是原生打款）： "+counterType);
        if(counterType.equalsIgnoreCase(SettleWithdrawalEnumConstants.ENUM_WITHDRAWALTYPE_FIRST)){
            //一清提现
            return this.ysWithdrawalService.createFirstWithdrawal(params);
        }else{
            //判断打款渠道
            String channel=sysconfigFeign.getCashChannel();
            if(channel.equalsIgnoreCase(SettleWithdrawalEnumConstants.ENUM_CASHCHANNEL_NATIVE)){
                return this.nativePaymentService.createNativeWithdrawal(params);
            }else{
                //非一清转账/打款
                return this.ysPaymentService.createWithdrawal(params);
            }
        }
    }*/
        //查询商户中心获取门店ID和ruleId
        String counterType = (String) params.get("ysUserStatus");
        logger.info("SettleUshopWithdrawalServiceImpl.createWithdrawal()", "该商户类型为（0是非一清代付;2是银盛一清;1是原生打款）： " + counterType);
        //查询金额类型：00一清现金;01一清现金+余额提现;02余额提现
        String withdrawalType = (String) params.get("withdrawalType");
        logger.info("SettleUshopWithdrawalServiceImpl.createWithdrawal()", "该提现类型为（00一清现金;01一清现金+余额提现;02余额提现）： " + withdrawalType);
        if (withdrawalType.equalsIgnoreCase(SettleWithdrawalEnumConstants.ENUM_TYPE_CASH)) {
            //一清现金支付
            if (counterType.equalsIgnoreCase(SettleWithdrawalEnumConstants.ENUM_WITHDRAWALTYPE_FIRST)) {
                //一清提现
                return this.ysWithdrawalService.createFirstWithdrawal(params);
            } else {
                //判断打款渠道
                String channel = sysconfigFeign.getCashChannel();
                if (channel.equalsIgnoreCase(SettleWithdrawalEnumConstants.ENUM_CASHCHANNEL_NATIVE)) {
                    return this.nativePaymentService.createNativeWithdrawal(params);
                } else {
                    //非一清转账/打款
                    return this.ysPaymentService.createWithdrawal(params);
                }
            }
        } else if (withdrawalType.equalsIgnoreCase(SettleWithdrawalEnumConstants.ENUM_TYPE_CASH_BALANCE)) {
            //一清现金+余额/积分支付
            return settleUshopFirstClearAndBalanceService.firstClearAndBalance(params);
        } else {
            //余额支付/积分
            return settleUshopBalanceAndIntegralService.balanceAndIntegral(params);
        }
    }
}